package com.ficus.road.maintain.handler.monitor;

import com.ficus.road.maintain.core.model.Car;
import com.ficus.road.maintain.core.model.Company;
import com.ficus.road.maintain.dao.CarDao;
import com.ficus.road.maintain.dao.ChecklistDao;
import com.ficus.road.maintain.dao.CompanyDao;
import com.ficus.road.maintain.dto.Checklist;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/5/23 下午5:08
 */
@Data
@Component
public abstract class AbstractMonitorHandler {

    static Map<String, Car> carMap = new HashMap<>();
    static Map<String, Company> companyMap = new HashMap<>();
    @Autowired
    CarDao carDao;
    @Autowired
    ChecklistDao checklistDao;
    @Autowired
    private CompanyDao companyDao;
    private AbstractMonitorHandler nextHandler;

    @PostConstruct
    void init() {
        List<Car> carList = carDao.findAll();
        carMap = carList.stream().collect(Collectors.toMap(Car::getId, Function.identity()));
        List<Company> companyList = companyDao.findAll();
        companyMap = companyList.stream().collect(Collectors.toMap(Company::getId, Function.identity()));
    }

    public void filter(Checklist checklist) {
        Checklist response = doFilter(checklist);
        if (getNextHandler() != null) {
            getNextHandler().filter(response);
        }
    }

    abstract Checklist doFilter(Checklist checklist);

}
